package com.zhiche.lisa.tms.controller;


import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.tms.dao.model.ShipmentAttach;
import com.zhiche.lisa.tms.service.IShipmentAttachService;
import com.zhiche.lisa.tms.vo.PhotoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 运单附件表 前端控制器
 * </p>
 *
 * @author qichao
 * @since 2018-08-16
 */
@RestController
@RequestMapping("/shipmentAttach")
@Api(value = "/shipmentAttach", description = "tms图片接口",tags={"tms图片接口"})
public class ShipmentAttachController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ShipmentController.class);

    @Autowired
    IShipmentAttachService shipmentAttachService;

    @PostMapping(value = "/photo")
    @ApiOperation(value = "图片保存", notes = "图片保存", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = String.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<String>  photo(@RequestBody PhotoVO photoVO,@RequestHeader("Authorization") String token) {
        LOGGER.info("ShipmentController.shiplistbydriverId（发车确认图片保存） params : {} ", photoVO);
        RestfulResponse result = new RestfulResponse<>(0,"success",null);
        try {
           shipmentAttachService.photo(photoVO,token);
        } catch (BaseException e) {
            LOGGER.error("ShipmentController.photo ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BankController.photo ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/updateAuthStatus")
    @ApiOperation(value = "更新图片审核状态", notes = "更新图片审核状态", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = String.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<String>  updateAuthStatus(@RequestBody Map<String, Object> code,@RequestHeader("Authorization") String token) {
        LOGGER.info("ShipmentController.shiplistbydriverId（更新图片审核状态） params : {} ", code);
        RestfulResponse result = new RestfulResponse<>(0,"success",null);
        try {
            shipmentAttachService.updateAttachAuthStatus(code,token);
        } catch (BaseException e) {
            LOGGER.error("ShipmentController.updateAuthStatus ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipmentController.updateAuthStatus ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/queryShipAttach")
    @ApiOperation(value = "根据装车CODE查看发运图片", notes = "根据装车CODE查看发运图片", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = String.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<List<ShipmentAttach>>  queryShipAttach(String code) {
        LOGGER.info("ShipmentController.queryShipAttach（根据装车CODE查看发运图片） params : {} ", code);
        RestfulResponse result = new RestfulResponse<>(0,"success",null);
        try {
            result.setData(shipmentAttachService.queryShipAttachList(code));
        } catch (BaseException e) {
            LOGGER.error("ShipmentController.queryShipAttach ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipmentController.queryShipAttach ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/getShipAttachUrl")
    @ApiOperation(value = "根据图片Key获取下载路径", notes = "根据图片Key获取下载路径", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = String.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<Map<String, Object>>  getShipAttachUrl(@RequestBody Map<String, Object> condition) {
        LOGGER.info("ShipmentController.getShipAttachUrl（根据图片Key获取下载路径） params : {} ", condition);
        RestfulResponse result = new RestfulResponse<>(0,"success",null);
        try {
            result.setData(shipmentAttachService.getShipAttachUrl(condition));
        } catch (BaseException e) {
            LOGGER.error("ShipmentController.getShipAttachUrl ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("ShipmentController.getShipAttachUrl ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/deleteShipAttach")
    @ApiOperation(value = "根据图片ID删除图片", notes = "根据图片ID删除图片", produces = MediaType.APPLICATION_JSON_VALUE,response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0,message = "success",response = Object.class),@ApiResponse(code = -1,message = "操作失败",response = Object.class)})
    public RestfulResponse<Object>  deleteShipAttach(Long id) {
        LOGGER.info("ShipmentController.deleteShipAttach（根据图片ID删除图片） params : {} ", id);
        RestfulResponse result = new RestfulResponse<>(0,"success",null);
        try {
            shipmentAttachService.deleteShipAttach(id);
        } catch (BaseException e) {
            LOGGER.error("ShipmentController.deleteShipAttach ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), false);
        } catch (Exception e) {
            LOGGER.error("ShipmentController.deleteShipAttach ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), false);
        }
        return result;
    }

}

